<?php
namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\Cache;
use think\Db;
use think\cache\driver\Redis;

/**
 * 系统配置
 * Class System
 * @package app\admin\controller
 */
class System extends AdminBase
{
    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 站点配置
     */
    public function siteConfig()
    {
        $site_config = Db::name('system')->field('value')->where('name', 'site_config')->find();
        $site_config = unserialize($site_config['value']);

        return $this->fetch('site_config', ['site_config' => $site_config]);
    }

    /**
     * 更新配置
     */
    public function updateSiteConfig()
    {
        if ($this->request->isPost()) {
            $site_config                = $this->request->post('site_config/a');
            $site_config['site_tongji'] = htmlspecialchars_decode($site_config['site_tongji']);
            $data['value']              = serialize($site_config);
            if (Db::name('system')->where('name', 'site_config')->update($data) !== false) {
                $this->success('提交成功');
            } else {
                $this->error('提交失败');
            }
        }
    }

    /**
     * 清除缓存
     */
    public function clear()
    {
        if (delete_dir_file(CACHE_PATH) || delete_dir_file(TEMP_PATH)) {
            $this->success('清除缓存成功');
        } else {
            $this->error('清除缓存失败');
        }
    }


    /**
     * 同步数据
     */
    public function same()
    {
        $config = [
            'host'       => '127.0.0.1',
            'port'       => 6379,
            'password'   => '',
            'select'     => 0,
            'timeout'    => 0,
            'expire'     => 0,
            'persistent' => false,
            'prefix'     => '',
        ];
        $redis = new Redis($config);
        //同步banner数据
        $first_bannser = Db::table('lsj_ads_banner')->select();
		foreach($first_bannser as $key=>$banner){
			$first_bannser[$key]['imgurl'] = "https://xfadmin.peddler.cn".$banner['imgurl'];
		}
        $redis->Hset('first_banner','data',$first_bannser);
        //同步首页通知数据
        $first_notice = Db::table('lsj_ads_notice')->select();
        $redis->Hset('first_notice','data',$first_notice);
        //同步类型数据
        $article_class = Db::table('lsj_article_class')->field('id,article_class')->select();
        $redis->Hset('article_class','data',$article_class);

        $this->success("同步成功");
    }

    /**
     * 分享配置
     */
    public function shareConfig()
    {
        $this->fetch("share_page");
    }
	
	/**
	 *  版本配置
     */	 
	public function versionConfig()
	{
		$config = Db::name('config')->find();
		return $this->fetch("version_config",['config'=>$config]);
	}
	
	/**
     * 更新分享配置
     */
    public function updateVersionConfig()
    {
        if ($this->request->isPost()) {
            $data                = $this->request->post();
			
            if (Db::name('config')->where('name','now_version')->update($data) !== false) {
                $this->success('提交成功');
            } else {
                $this->error('提交失败');
            }
        }
    }
}